﻿///
/// Pamela.Logic.ProjectFiles
///
namespace Pamela.Logic.ProjectFiles
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;

    using Pamela.Logic.Exceptions;
    using Pamela.Logic.Helpers;
    using Pamela.Logic.ProjectFiles.Base;
    using Pamela.Logic.Properties;    

    /// <summary>
    /// ProjectFileApplication class
    /// </summary>
    internal class ProjectFileApplication : ProjectFileBase
    {
        /// <summary>
        /// Initializes a new instance of the ProjectFileApplication class
        /// </summary>
        /// <param name="info">Builder object containing all the info to necessary perform the scaffolding operation</param>
        public ProjectFileApplication(BuilderInfo info)
            : base(info)
        {
            this.path = string.Format("{0}\\{1}.App\\{1}.App.csproj", info.SolutionInnerDirectory, info.RootNamespace);
            this.lines = File.ReadAllLines(this.path).ToList();
        }

        /// <summary>
        /// Validates if the Visual Studio project file already contains entries for the files that will be included
        /// </summary>
        /// <returns>Returns true if there are no entries</returns>
        public override bool Validate()
        {            
            for (int i = 0; i < lines.Count(); i++)
            {
                if (lines[i].Contains(string.Format(Resources.ControllerContentIncludeAddChstml, this.selectedModel)) ||
                    lines[i].Contains(string.Format(Resources.ControllerContentIncludeEditChstml, this.selectedModel)) ||
                    lines[i].Contains(string.Format(Resources.ControllerContentIncludeIndexChstml, this.selectedModel)))
                {
                    return false;
                }
            }

            return this.HasBeenValidated = true;
        }

        /// <summary>
        /// Updates the Visual Studio project file to reflect the additions of the scaffold code
        /// </summary>
        public override void Update()
        {
            if (!this.HasBeenValidated)
            {
                throw new ScaffoldingException("Valdiation needs to be performed");
            }

            for (int i = 0; i < lines.Count(); i++)
            {
                if (lines[i].Contains(Resources.ContainsCompileInclude))
                {
                    lines.Insert(i, string.Format(Resources.ControllersCompileInclude, this.selectedModel));
                    break;
                }
            }

            for (int i = 0; i < lines.Count(); i++)
            {
                if (lines[i].Contains(Resources.ContainsContentInclude))
                {
                    lines.Insert(i, string.Format(Resources.ControllerContentIncludeAddChstml, this.selectedModel));
                    lines.Insert(i, string.Format(Resources.ControllerContentIncludeEditChstml, this.selectedModel));
                    lines.Insert(i, string.Format(Resources.ControllerContentIncludeIndexChstml, this.selectedModel));
                    break;
                }
            }
        }
    }
}
